#! /bin/bash
export LANG=en_US.UTF-8

swap_total_mem_min=99999999990
swap_total_mem_max=99999999999
swap_total_mem=0

swap_used_mem_min=
swap_used_mem_max=
swap_used_mem=0

swap_free_mem_min=
swap_free_mem_max=
swap_free_mem=0

swap_use_per_min=20
swap_use_per_max=70
swap_use_per=0

total_mem_min=99999999990
total_mem_max=99999999999
total_mem=0

used_mem_min=
used_mem_max=
used_mem=0

free_mem_min=
free_mem_max=
free_mem=0

mem_used_per_min=20
mem_used_per_max=70
mem_used_per=0


load1min=
load1max=
load1=0
load5min=
load5max=
load5=0
load15min=
load15max=
load15=0

StatStr=
CURNUM=				#当前的性能参数

WranResultStr=		#最后形成的告警字符串
ErrorResultStr=		#最后形成的错误字符串
DataResultStr=		#性能呢个参数字符串

quota=
QUOTASTR=

function gotErr(){
	if [ "$1" -eq 0 ];then
		echo "mailstatedes=各项指标恢复正常 statedes=各项指标恢复正常|$DataResultStr"
	elif [ "$1" -eq 1 ];then 
		echo "quota=${quota} mailstatedes=$WranResultStr statedes=$WranResultStr ${QUOTASTR} |$DataResultStr"
	elif [ "$1" -eq 2 ];then 
		if [[ "${WranResultStr}" != "" ]]; then
			echo "quota=${quota} mailstatedes=${WranResultStr},${ErrorResultStr} statedes=${WranResultStr},${ErrorResultStr} ${QUOTASTR}|$DataResultStr"
			else
			echo "quota=${quota} mailstatedes=${ErrorResultStr} statedes=${ErrorResultStr} ${QUOTASTR}|$DataResultStr"
		fi
		
	elif [ "$1" -eq 3 ];then 
		echo "mailstatedes=无法获取到指标,疑似组件故障,请确认并请检查监控脚本和运维平台配置 statedes=无法获取到指标,疑似组件故障,请确认并请检查监控脚本和运维平台配置"
	fi

	exit $1
}

function contain(){
	result=$(echo $1 | grep "$2")
	if [[ "$result" != "" ]]
	then
		return 1
	else
		return 0
	fi
}


swap_total_mem_value=0
swap_used_mem_value=0
swap_free_mem_value=0

total_mem_value=0
used_mem_value=0
free_mem_value=0
#依据传入的参数值来判断我们要监控的服务是什么
#然后通过各个函数获取到具体的值
#值之间的
function analysisStat(){
swap_total_mem_value=`free -m |awk 'NR==4{print $2}'`
swap_used_mem_value=`free -m |awk 'NR==4{print $3}'`
swap_free_mem_value=`free -m |awk 'NR==4{print $4}'`

total_mem_value=`free -m |awk 'NR==2{print $2}'`
used_mem_value=`free -m |awk 'NR==2{print $3}'`
free_mem_value=`free -m |awk 'NR==2{print $4}'`
buffers_mem_value=`free -m |awk 'NR==2{print $6}'`
cached_mem_value=`free -m |awk 'NR==2{print $7}'`
buffers_cached_value=$(( $buffers_mem_value + $cached_mem_value ))
used_mem_value=$(( $used_mem_value - $buffers_cached_value ))
free_mem_value=$(( $used_mem_value + $buffers_cached_value ))


CURNUM=$swap_total_mem_value
	if [ "$swap_total_mem" -eq 1 ];then
		parseFileds "swap_total_mem" $swap_total_mem_min $swap_total_mem_max
	fi

CURNUM=$swap_used_mem_value
	if [ "$swap_used_mem" -eq 1 ];then
		parseFileds "swap_used_mem" $swap_used_mem_min $swap_used_mem_max
	fi

CURNUM=$swap_free_mem_value
	if [ "$swap_free_mem" -eq 1 ];then
		parseFileds "swap_free_mem" $swap_free_mem_min $swap_free_mem_max
	fi
CURNUM=`awk 'BEGIN{printf "%.2f\n",('$swap_used_mem_value'/'$swap_total_mem_value')*100}'`
	if [ "$swap_use_per" -eq 1 ];then
		parseFileds "swap_use_per" $swap_use_per_min $swap_use_per_max
	fi
CURNUM=$total_mem_value
	if [ "$total_mem" -eq 1 ];then
		parseFileds "total_mem" $total_mem_min $total_mem_max
	fi
CURNUM=$used_mem_value
	if [ "$used_mem" -eq 1 ];then
		parseFileds "used_mem" $used_mem_min $used_mem_max
	fi
CURNUM=$free_mem_value
	if [ "$free_mem" -eq 1 ];then
		parseFileds "free_mem" $free_mem_min $free_mem_max
	fi
CURNUM=`awk 'BEGIN{printf "%.2f\n",('$used_mem_value'/'$total_mem_value')*100}'`
	if [ "$mem_used_per" -eq 1 ];then
		parseFileds "mem_used_per" $mem_used_per_min $mem_used_per_max
	fi   
	
	CURNUM=$buffers_cached_value
	parseFileds "cached_mem" $CURNUM $CURNUM
}

#CURNUM			当前值
#WARNLIMIT 		告警的上限
#ERRORLIMIT 	异常的上限
#判断以上三个值并拼接结果字符串
function parseFileds(){
	val1=$(echo "$CURNUM $2"| awk '{if($1<=$2){print 1}else{print 0}}')
	val2=$(echo "$CURNUM $3"| awk '{if($1<=$2){print 1}else{print 0}}')

	DataResultStr="$DataResultStr $1=$CURNUM;$2;$3;0;0"
	if [ $val1 -eq 1 ];then
		return 0
	elif [[ $val1 -eq 0 && $val2 -eq 1 ]];then
		if [[ ${WranResultStr} != "" ]]; then
			WranResultStr="${WranResultStr},"
		fi

		WranResultStr="${WranResultStr}$1当前值为${CURNUM}超过告警值${2}"

		if [[ ${quota} != "" ]]; then
			quota="${quota},"
		fi

		quota="${quota}$1"

		QUOTASTR="${QUOTASTR}$1=$1当前值为${CURNUM}超过告警值${2} "

		return 1
	else
		if [[ ${ErrorResultStr} != "" ]]; then
			ErrorResultStr="${ErrorResultStr},"
		fi
		ErrorResultStr="${ErrorResultStr}$1当前值为${CURNUM}超过紧急值${3}"

		if [[ ${quota} != "" ]]; then
			quota="${quota},"
		fi

		quota="${quota}$1"

		QUOTASTR="${QUOTASTR}$1=$1当前值为${CURNUM}超过紧急值${3} "
		
		return 2
	fi
}

#分析结果 给出给出状态
function analysisResult(){
	if [[ "$WranResultStr" == "" && "$ErrorResultStr" == "" ]];then
		gotErr 0
	elif [[ "$ErrorResultStr" == "" ]];then
		gotErr 1
	else
		gotErr 2
	fi
}

#输出错误信息并且退出程序
function showHelp(){
	if [ "$1" != "" ];then
		echo "请输入$1"
	fi
	echo "check_mem.sh 可以监听本地的memory的状态 参数如下"
	echo "check_mem.sh [-w swap_total_mem,swap_used_mem,swap_free_mem,swap_use_per,total_mem,used_mem,free_mem,mem_used_per] [-c swap_total_mem,swap_used_mem,swap_free_mem,swap_use_per,total_mem,used_mem,free_mem,mem_used_per]"
	echo "-w 表示监控警告的值"
	echo "-c 表示异常值"
	exit 1
}

#检查参数是不是数字
function isNumber(){
	isNum=$(echo "$1" | awk '{print($0~/^([0-9])+[\.]?([0-9])*$/)?1:0}')
	return $isNum
}

function isLarge(){

	isNumber $1
	if [[ $? -eq 0 ]]; then
		return 1
	fi

	isNumber $2
	if [[ $? -eq 0 ]]; then
		return 1
	fi

	isLarge=$(echo "$1 $2"| awk '{if($1>=$2){print 1}else{print 0}}')
	if [ $isLarge -eq 1 ];then
		return 0
	else
		return 1
	fi
}

function checkARG(){
	if [[ "$2" != "" && "$3" != "" ]];then
		isLarge "$3" "$2"
		if [[ $? -eq 1 ]]; then
			return 1
		else
			$1=1
		fi
	fi
	return 0
}

#检查参数是否合法
#即参数是否是小数和整数
#对于守护进程的选择 这个值可以不验证
function checkALL(){

	if [[ "$swap_total_mem_min" != "" || "$swap_total_mem_max" != "" ]];then
		if [[ "$swap_total_mem_max" == "" || "$swap_total_mem_min" == "" ]]; then
			return 1
		fi
		
		isLarge "$swap_total_mem_max" "$swap_total_mem_min"
		if [[ $? -eq 1 ]]; then
			return 1
		else
			swap_total_mem=1
		fi
	fi
	if [[ "$swap_used_mem_min" != "" || "$swap_used_mem_max" != "" ]];then
		if [[ "$swap_used_mem_max" == "" || "$swap_used_mem_min" == "" ]]; then
			return 1
		fi
		
		isLarge "$swap_used_mem_max" "$swap_used_mem_min"
		if [[ $? -eq 1 ]]; then
			return 1
		else
			swap_used_mem=1
		fi
	fi


	if [[ "$swap_free_mem_min" != "" || "$swap_free_mem_max" != "" ]];then
		if [[ "$swap_free_mem_max" == "" || "$swap_free_mem_min" == "" ]]; then
			return 1
		fi

		isLarge "$swap_free_mem_max" "$swap_free_mem_min"
		if [[ $? -eq 1 ]]; then
			return 1
		else
			swap_free_mem=1
		fi
	fi

	if [[ "$swap_use_per_min" != "" || "$swap_use_per_max" != "" ]];then
		if [[ "$swap_use_per_max" == "" || "$swap_use_per_min" == "" ]]; then
			return 1
		fi

		isLarge "$swap_use_per_max" "$swap_use_per_min"
		if [[ $? -eq 1 ]]; then
			return 1
		else
			swap_use_per=1
		fi
	fi
	if [[ "$total_mem_min" != "" || "$total_mem_max" != "" ]];then
		if [[ "$total_mem_max" == "" || "$total_mem_min" == "" ]]; then
			return 1
		fi

		isLarge "$total_mem_max" "$total_mem_min"
		if [[ $? -eq 1 ]]; then
			return 1
		else
			total_mem=1
		fi
	fi



	if [[ "$used_mem_min" != "" || "$used_mem_max" != "" ]];then
		if [[ "$used_mem_max" == "" || "$used_mem_min" == "" ]]; then
			return 1
		fi

		isLarge "$used_mem_max" "$used_mem_min"
		if [[ $? -eq 1 ]]; then
			return 1
		else
			used_mem=1
		fi
	fi

	if [[ "$free_mem_min" != "" || "$free_mem_max" != "" ]];then
		if [[ "$free_mem_max" == "" || "$free_mem_min" == "" ]]; then
			return 1
		fi

		isLarge "$free_mem_max" "$free_mem_min"
		if [[ $? -eq 1 ]]; then
			return 1
		else
			free_mem=1
		fi
	fi
    
    if [[ "$mem_used_per_min" != "" || "$mem_used_per_max" != "" ]];then
		if [[ "$mem_used_per_max" == "" || "$mem_used_per_min" == "" ]]; then
			return 1
		fi

		isLarge "$mem_used_per_max" "$mem_used_per_min"
		if [[ $? -eq 1 ]]; then
			return 1
		else
			mem_used_per=1
		fi
	fi
    
	return 0
}



function parseARG(){
	#it mean warning
	if [ $1 -eq 1 ];then
 		#load1min=$(echo $2 | awk -F ',' '{print $1}' | sed "s/[0-9]\.[0-9]//g")
 		swap_total_mem_min=$(echo $2 | awk -F ',' '{print $1}' | sed "s/[^0-9\.]//g")
		swap_used_mem_min=$(echo $2 | awk -F ',' '{print $2}' | sed "s/[^0-9\.]//g")
		swap_free_mem_min=$(echo $2 | awk -F ',' '{print $3}' | sed "s/[^0-9\.]//g")
		swap_use_per_min=$(echo $2 | awk -F ',' '{print $4}' | sed "s/[^0-9\.]//g")
		total_mem_min=$(echo $2 | awk -F ',' '{print $5}' | sed "s/[^0-9\.]//g")
		used_mem_min=$(echo $2 | awk -F ',' '{print $6}' | sed "s/[^0-9\.]//g")
		free_mem_min=$(echo $2 | awk -F ',' '{print $7}' | sed "s/[^0-9\.]//g")
    mem_used_per_min=$(echo $2 | awk -F ',' '{print $8}' | sed "s/[^0-9\.]//g")
 		
 	else
 		swap_total_mem_max=$(echo $2 | awk -F ',' '{print $1}' | sed "s/[^0-9\.]//g")
		swap_used_mem_max=$(echo $2 | awk -F ',' '{print $2}' | sed "s/[^0-9\.]//g")
		swap_free_mem_max=$(echo $2 | awk -F ',' '{print $3}' | sed "s/[^0-9\.]//g")
		swap_use_per_max=$(echo $2 | awk -F ',' '{print $4}' | sed "s/[^0-9\.]//g")
		total_mem_max=$(echo $2 | awk -F ',' '{print $5}' | sed "s/[^0-9\.]//g")
		used_mem_max=$(echo $2 | awk -F ',' '{print $6}' | sed "s/[^0-9\.]//g")
		free_mem_max=$(echo $2 | awk -F ',' '{print $7}' | sed "s/[^0-9\.]//g")
    mem_used_per_max=$(echo $2 | awk -F ',' '{print $8}' | sed "s/[^0-9\.]//g")
 	fi

}

while getopts "w:c:h" arg
do
	case $arg in
		w)
			parseARG 1 "$OPTARG"
			;;
		c)
			parseARG 2 "$OPTARG"
			;;
		h)
			showHelp
			;;
		?) 
			showHelp
			;;
	esac
done

checkALL
if [[ $? == 1 ]];then
	showHelp "正确参数"
fi


#插件正题部分 
#确定存活的
#启动
#分析
analysisStat
analysisResult
